import {
	getDialogs,
	setDialogStatus,
	deleteDialog,
	addDialog,
} from "@/services/dialog";
import { routerRedux } from "dva/router";
import UserSession from "@/UserSession";
export default {
	namespace: "dialogList",
	state: {
		total: null,
		data: [],
		loading: false,
	},

	effects: {
		*fetchList({ payload, callback }, { call, put }) {
			const response = yield call(getDialogs, payload);
			if (callback && typeof callback === "function") {
				callback(response);
			}
			yield put({
				type: "save",
				payload: response,
			});
		},
		*fixDialogStatus({ payload, callback }, { call, put }) {
			const response = yield call(setDialogStatus, payload);
			if (callback && typeof callback === "function") {
				callback(response);
			}
			yield put({
				type: "save",
				payload: response,
			});
		},
		*delDialog({ payload, callback }, { call, put }) {
			const response = yield call(deleteDialog, payload);
			if (callback && typeof callback === "function") {
				callback(response);
			}
			yield put({
				type: "save",
				payload: response,
			});
		},
		*redirect({ payload }, { put }) {
			const { pathId } = payload;
			if (UserSession.isSystemKnowledge()) {
				yield put(
					routerRedux.push({
						pathname: `/knowledgeDesign/systemKnowledge/${pathId}/dialog/add`,
						query: payload,
					})
				);
			} else {
				yield put(
					routerRedux.push({
						pathname: `/knowledgeDesign/myKnowledge/${pathId}/dialog/add`,
						query: payload,
					})
				);
			}
		},
		/** speicalCaseRedirect ({ payload }, { put }) {
      yield put(routerRedux.push('/dialog/specialIndex', payload));
    },*/
		*add({ payload, callback }, { call, put }) {
			const response = yield call(addDialog, payload);
			if (callback && typeof callback === "function") {
				callback(response);
			}
			yield put({
				type: "save",
				payload: response,
			});
		},
	},

	reducers: {
		save(state, { payload }) {
			return {
				...state,
				...payload,
			};
		},
	},
};
